﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Com.OpenCode.Framework.Core;
using DevExpress.XtraBars;
using DevExpress.XtraEditors.Repository;

namespace Com.OpenCode.Framework.DevForm
{
    /// <summary>
    ///     右键菜单。
    /// </summary>
    public class OpenCodeRightMenuUI : PopupMenu
    {
        #region 构造函数

        /// <summary>
        ///     右键菜单构造函数。
        /// </summary>
        public OpenCodeRightMenuUI(BarManager barManager)
        {
            Manager = barManager;
        }

        public OpenCodeRightMenuUI()
        {
        }

        #endregion

        #region 属性

        /// <summary>
        ///     当前菜单绑定对象。
        /// </summary>
        public object Tag { get; set; }

        /// <summary>
        ///     菜单所属。
        /// </summary>
        public Control Parent { get; set; }

        #endregion

        #region Public Method

        public virtual BarItem CreateMenuItem(string caption, ItemClickEventHandler handle = null, Image image = null)
        {
            BarItem barItem = new BarButtonItem(Manager, caption);
            barItem.PaintStyle = BarItemPaintStyle.CaptionGlyph;
            barItem.ItemClick += handle;
            if (image==null)
            {
                image = DevFormImage.RightMenu16;
            }
            barItem.Glyph = image;
            return barItem;
        }

        /// <summary>
        ///     添加菜单项。
        /// </summary>
        /// <param name="caption">菜单项显示名称</param>
        /// <param name="handle">相应事件。</param>
        /// <param name="image"></param>
        public virtual BarItem AddMenuItem(string caption, ItemClickEventHandler handle = null, Image image = null)
        {
            BarItem barItem = CreateMenuItem(caption, handle, image);
            AddItem(barItem);
            return barItem;
        }

        public virtual BarItem AddMenuItem(string caption, ItemClickEventHandler handle = null, Addin addin = null, string image = "")
        {
            Image obj = null;
            if (addin != null)
            {
                Plugin plugin = addin.Plugin.Search("Icon", "Id", image);
                if (plugin != null)
                {
                    obj = (Image)addin.Bulid(plugin, null);
                }
            }
            return AddMenuItem(caption, handle, obj);
        }

        /// <summary>
        ///     添加菜单项。
        /// </summary>
        /// <param name="caption">菜单项显示名称</param>
        /// <param name="handle"></param>
        /// <param name="image"></param>
        /// <param name="beginGroup">是否beginGroup</param>
        public virtual BarItem AddMenuItem(string caption, ItemClickEventHandler handle = null, Image image = null, bool beginGroup = false)
        {
            BarItem barItem = CreateMenuItem(caption, handle, image);
            AddItem(barItem).BeginGroup = beginGroup;
            return barItem;
        }

        public virtual BarItem AddMenuItem(string caption, ItemClickEventHandler handle = null, Addin addin = null, string image = "", bool beginGroup = false)
        {
            Image obj = null;
            if (addin != null)
            {
                Plugin plugin = addin.Plugin.Search("Icon", "Id", image);
                if (plugin != null)
                {
                    obj = (Image)addin.Bulid(plugin, null);
                }
            }
            return AddMenuItem(caption, handle, obj, beginGroup);
        }

        /// <summary>
        /// 添加文本输入框。
        /// </summary>
        /// <returns></returns>
        public virtual BarEditItem AddTextEditItem(string caption,KeyEventHandler handle = null, EventHandler changedHandle = null, int width = 70, bool beginGroup = false)
        {
            BarEditItem item = new BarEditItem(Manager)
            {
                Width = width,
                Caption = caption,
                PaintStyle = BarItemPaintStyle.CaptionGlyph
            };
            RepositoryItemTextEdit edit = new RepositoryItemTextEdit();
            edit.KeyDown += handle;
            edit.EditValueChanged += changedHandle;
            item.Edit = edit;
            AddItem(item).BeginGroup = beginGroup;
            return item;
        }

        /// <summary>
        ///     添加子菜单。
        /// </summary>
        /// <param name="caption">子菜单显示名称</param>
        /// <param name="imageIndex">子菜单图标索引</param>
        /// <param name="barItems">子菜单下菜单项列表</param>
        public void AddSubMenu(string caption, int imageIndex, BarItem[] barItems)
        {
            BarItem subMenu = new BarSubItem(Manager, caption, imageIndex, barItems);
            AddItem(subMenu);
        }

        /// <summary>
        ///     添加子菜单。
        /// </summary>
        /// <param name="caption">子菜单显示名称</param>
        /// <param name="imageIndex">子菜单图标索引</param>
        /// <param name="barItems">子菜单下菜单项列表</param>
        /// <param name="beginGroup">是否开始分组</param>
        public BarSubItem AddSubMenu(string caption, int imageIndex, BarItem[] barItems, bool beginGroup = false)
        {
            BarSubItem subMenu = new BarSubItem(Manager, caption, imageIndex, barItems);
            AddItem(subMenu).BeginGroup = beginGroup;
            return subMenu;
        }

        /// <summary>
        ///  添加子菜单。
        /// </summary>
        public BarSubItem AddSubMenu(string caption, int imageIndex, CustomBarItem[] barItems, bool beginGroup = false)
        {
            BarSubItem subMenu = new BarSubItem(Manager, caption, imageIndex);
            if (barItems != null && barItems.Length > 0)
            {
                for (int i = 0; i < barItems.Length; i++)
                {
                    CustomBarItem item = barItems[i];
                    if (item != null)
                    {
                        BarItem barItem = CreateMenuItem(item.Caption, item.ItemClick, item.Image);
                        subMenu.AddItem(barItem).BeginGroup = item.BeginGroup;
                    }
                }
            }
            AddItem(subMenu).BeginGroup = beginGroup;
            return subMenu;
        }


        public virtual void Update()
        {
        }

        public override void ShowPopup(Point p)
        {
            foreach (BarItemLink item in ItemLinks)
            {
                if (item is BarEditItemLink)
                {
                    item.Visible = false;
                }
            }
            base.ShowPopup(p);
        }

        #endregion
    }

    /// <summary>
    ///     自定义菜单项。
    /// </summary>
    public class CustomBarItem
    {
        public CustomBarItem()
        {
        }

        public CustomBarItem(string caption, ItemClickEventHandler handle = null,
            Image image = null, bool beginGroup = false)
        {
            Caption = caption;
            ItemClick = handle;
            Image = image;
            BeginGroup = beginGroup;
        }

        public string Caption { get; set; }

        public Image Image { get; set; }

        /// <summary>
        ///     获取或设置是否开始分组。
        /// </summary>
        public bool BeginGroup { get; set; }

        public ItemClickEventHandler ItemClick { get; set; }
    }
}